package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    /**
     * 查询默认地址
     */
    public List<AddressBook> showDefaultAddressBook() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .isDefault(1)
                .build();
        List<AddressBook> isDefaultAddressList = mapper.showAddressBook(addressBook);
        return isDefaultAddressList;
    }

    /**
     * 新增地址簿
     * @param addressBook
     */
    public void addAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);

        mapper.addAddressBook(addressBook);
    }

    /**
     * 根据id查询地址簿
     * @param id
     * @return
     */
    public List<AddressBook> AddressBookById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
       /* Long userId = BaseContext.getCurrentId();
        addressBook.setUserId();*/
        List<AddressBook> list = mapper.showAddressBook(addressBook);
        return list;
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        mapper.updateAddressBook(addressBook);
    }

    @Override
    public void updateDefaultAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        //首先修改地址簿所有默认状态，默认为0
        mapper.updateDefaultAddressBook(addressBook);

        addressBook.setIsDefault(1);
    }

    @Override
    public void deleteAddressBook(Long id) {
        mapper.deleteAddressBook(id);
    }

    @Autowired
    private AddressMapper mapper;
    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    public List<AddressBook> showAddressBook() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .build();
        List<AddressBook> list = mapper.showAddressBook(addressBook);
        return list;
    }
}
